package gold.digger;

import gold.utils.InputUtil;
import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LCIT0406 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        public boolean isFind = false;
        public TreeNode next = null;

        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            inorderSuccessorRecur(root, p);
            return next;
        }

        public void inorderSuccessorRecur(TreeNode root, TreeNode p) {
            if (null == root || null != next) return;
            inorderSuccessorRecur(root.left, p);
            if (root == p) isFind = true;
            if (null != next) return;
            if (root != p && isFind && null != root) {
                next = root;
                return;
            }
            inorderSuccessorRecur(root.right, p);
        }
    }

    public void run() {
//        TreeNode root = TreeNode.createTree("[2,1,3]");
//        System.out.println(new Solution().inorderSuccessor(root, root.left));

        TreeNode root = TreeNode.createTree("[5,3,6,2,4,null,null,1]");
        System.out.println(new Solution().inorderSuccessor(root, root.left.left.left));
    }

    public static void main(String[] args) throws Exception {
        LCIT0406 an = new LCIT0406();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
